#pragma once
#include "cocos2d.h"
#include "PhysicalWorld.h"
#include "SpriteObject.h"
#include "ParallaxLayer.h"
#include <vector>
#include <deque>
class GameLayer:public PhysicalWorld
{
public:
	GameLayer(void);
	~GameLayer(void);
	static GameLayer *node();
private: 
	
	bool initParallaxNodes();
	bool initPlayer();
	bool initBlood();
	bool initWall();
	bool initBg(ParallaxLayer *pParent);
	

	
	bool initDistance();
	bool initStarScore();
	bool initStatebar();
protected:
	bool init();
	virtual void update(float fTime);
	virtual void beginContact(cocos2d::CCNode *pNodeA,cocos2d::CCNode *pNodeB);
	virtual void preSolveContact(cocos2d::CCNode *pNodeA,cocos2d::CCNode *pNodeB);
	virtual void raycastPlayerCollision();
	void  PlayerWallCollision(cocos2d::CCNode *pPlayerNode,cocos2d::CCNode *pWallNode);
	void  PlayerMonsterCollision(cocos2d::CCNode *pPlayerNode,cocos2d::CCNode *pMonsterNode);

	bool ccTouchBegan( cocos2d::CCTouch* touche, cocos2d::CCEvent* event );
	void ccTouchMoved( cocos2d::CCTouch* touche, cocos2d::CCEvent* event );
	void ccTouchEnded( cocos2d::CCTouch* touche, cocos2d::CCEvent* event );
	void registerWithTouchDispatcher();

	void updateParaxLayer(float fTime);
	void updatePlayer(float fTime);
	void updateDistance(float fTime);
	virtual void draw();
	
private:
	bool addMonster(MONSTER_SUB_TAG subTag, const cocos2d::CCPoint &pt,ParallaxLayer *pParent);
	bool addObstacle(const cocos2d::CCPoint &pt,ParallaxLayer *pParent);
	

	void removeSpriteObject(SpriteObject *pObj);
	void removeAllChild();

	
	void setPlayerTouchSpeed();

	bool shouldProduceObject();
	bool shouldAddBlood();
	void ProduceObject();
	void ProduceStar(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceBlackCloud(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceEagle(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceFlashing(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProducePlane(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceSafecap(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceArmor(int nNum,std::vector<std::pair<int,int> >&netGridding);
	void ProduceGhost(int nNum,std::vector<std::pair<int,int> >&netGridding);

	bool DealPlayerArossUpLine();


	void collisionStar();
	void collisionBlackcloud();
	void collisionEagle();
	void collisionFlashing();
	void collisionPlane();
	void collisionSafecap();
	void collisionArmor();
	void collisionGhost();

public:
	bool safecapClick();
	bool armorClick();
	bool ghostClick();
private:
	void planeEnd(cocos2d::CCNode *pNode);
private:
	float fMousemoveDistance_;
	cocos2d::CCPoint ptLast_;
	cocos2d::CCPoint ptDirection_;
	timeval mouseMoveStartTime_;
	bool  bTouchPlayer_;


	cocos2d::CCPoint ptStart_;
	cocos2d::CCPoint ptEnd_;
	bool bMouseMove_;

	float fEdgeDistance_;
	float fBloodDistance_;
	std::list<cocos2d::CCPoint> allTouchPt_;
private:
	void initDebugLable();
	void drawDebugLine(cocos2d::CCPoint pt1,cocos2d::CCPoint pt2);
	
	
	


};
